/*******************************************************************************

[Last updated: June 4th, 2024]

This script is the second one in the runny sequence for the Endline data analysis

The script is organized similar to that of the admin analysis, but focus solely
on performing FDR adjustments. The structure of the script is as follows:

	PART A: Creating Families
	PART B: Calculate q-values

Results from this step are also stored in Stata's memory

*******************************************************************************/




* PART A: Creating Families ----------------------------------------------------
	
	* Family 1: Knowledge Family
	gl knowledge_fam  knowledge assailants_correct force_correct z_q50_level

	* Family 2: Ambiguity family
	gl ambiguity_fam  total_interpretations dummy_twocategories dummy_A dummy_E dummy_NA ///
				zall_features_unsel zall_features_sel ///
				proccessing_log_time t_overall ///
				overall ///
				appropriate_action2 ///
				z_appropriate z_nonappropriate ///
				personalization ///
				emotion stress policing
				
	* Family 3: Performance in the FOS
	gl performance_fam  /// 
		communicate verbalize radio freeze moving_cover shooting_FOS ///
		item clarity 

		* For shooting_FOS, need to handcode because it comes from interaction regression
		foreach t in N H L {
			mat `t'_shooting_FOS = F`t'_shooting_FOS_treatment_risk
			mat `t'_FOS_treat = F`t'_shooting_FOS_treatment
			mat `t'_FOS_risk = F`t'_shooting_FOS_risk
		}
	
	* Other families (for robustness check purposes)
	gl sub_knowledge_fam 			q3_score q4_score q5_score q6_score q8_score 
	gl sub_stress_fam 				q17_1_score q17_2_score q17_3_score 
	gl sub_emotion_fam				q15_1_score q15_2_score q15_3_score 
	gl sub_others					z_q53121 z_q55123 z_q56124 
	gl sub_personalization			q20_score q71_score q75_score q77_score q80_score 
	gl sub_confidence 				q13_1_score q13_2_score q13_3_score q13_4_score q13_5_score 
	gl sub_post						q3_correct q5_pricorrect
	gl phantom						FOS_treat FOS_risk
	
	* All families
	gl all_fam	 	knowledge_fam ambiguity_fam performance_fam ///
					sub_knowledge_fam sub_stress_fam sub_emotion_fam ///
					sub_others sub_personalization sub_confidence sub_post phantom

* Part B: Run and store q-values -----------------------------------------------

	* Make families for each specification
		foreach fam in $all_fam {
		
		foreach spec in N H L {
		gl l_`spec'_`fam' 
		
		foreach member in ${`fam'}{
			gl l_`spec'_`fam' ${l_`spec'_`fam'} `spec'_`member' 
			
		}
		di "${l_`spec'_`fam'}"
		}
	}

	* Calculate and store q_values
	foreach fam in $all_fam {
		
		foreach spec in N H L {
		gl family ${l_`spec'_`fam'}
		di "$family"
		p_to_q
		}
	}
	
	* Save to memory
	mat memory = J(1, 6, .)
		foreach fam in $all_fam {
		foreach spec in N H L {
			foreach member in  ${l_`spec'_`fam'} {
		mat add_mem = q_`member'
		mat rownames add_mem = q_`member'
		mat memory = memory \ add_mem
		}
		}
	}
	mat li memory
	
	
	mat colnames memory = CM COEF SE P Q N
	putexcel set ${textable}/memory, replace
	putexcel A1=matrix(memory), names